﻿using System;
using System.Net;
using Newtonsoft.Json;

namespace CRM.Anyleads.ResponseStruct
{
    public class UrlDataJson
    {
        public string Query { get; set; }
        public string Domain { get; set; }
        public string Title { get; set; }
        public object Description { get; set; }
        public object Keywords { get; set; }
        public string Phones { get; set; }
        [JsonProperty("twitter_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object TwitterUrl { get; set; }
        [JsonProperty("twitter_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object TwitterUsername { get; set; }
        [JsonProperty("facebook_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object FacebookUrl { get; set; }
        [JsonProperty("facebook_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object FacebookUsername { get; set; }
        [JsonProperty("google_plus_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object GooglePlusUrl { get; set; }
        [JsonProperty("google_plus_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object GooglePlusUsername { get; set; }
        [JsonProperty("youtube_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object YoutubeUrl { get; set; }
        [JsonProperty("youtube_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object YoutubeUsername { get; set; }
        [JsonProperty("instagram_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object InstagramUrl { get; set; }
        [JsonProperty("instagram_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object InstagramUsername { get; set; }
        [JsonProperty("linkedin_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object LinkedinUrl { get; set; }
        [JsonProperty("linkedin_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object LinkedinUsername { get; set; }
        [JsonProperty("github_url"), JsonConverter(typeof(KeysJsonConverter))]
        public object GithubUrl { get; set; }
        [JsonProperty("github_username"), JsonConverter(typeof(KeysJsonConverter))]
        public object GithubUsername { get; set; }
        [JsonProperty("company_email")]
        public string CompanyEmail { get; set; }
    }
    
    public class KeysJsonConverter : JsonConverter
    {

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value is string str)
            {
                return WebUtility.HtmlDecode(str);
            }

            return reader.Value;
        }

        public override bool CanWrite => false;

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(string);
        }
    }
}